package base;

import config.ApiConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static base.BaseTest.getPlaceholderValue;


public class PlaceholderResolver {
    // 处理字符串占位符
    public static String resolvePlaceholders(String value) {
        if (value == null) {
            return null;
        }

        // 这里可以实现具体占位符解析逻辑
        // 例如处理 ${variable} 格式的占位符
        Pattern pattern = Pattern.compile("\\$\\{([^}]+)\\}");
        Matcher matcher = pattern.matcher(value);
        StringBuffer result = new StringBuffer();

        while (matcher.find()) {
            String placeholder = matcher.group(1);
            String replacement = getPlaceholderValue(placeholder);
            if (replacement != null) {
                // 对替换字符串进行转义处理，避免特殊字符导致错误
                matcher.appendReplacement(result, Matcher.quoteReplacement(replacement));
            } else {
                // 如果找不到占位符的值，保留原样或提供默认值
                matcher.appendReplacement(result, Matcher.quoteReplacement(matcher.group(0)));
            }
        }
        matcher.appendTail(result);

        return result.toString();
    }

    // 处理 Map 中的占位符
    public static Map<String, Object> resolvePlaceholdersInMap(Map<String, Object> map) {
        if (map == null) {
            return null;
        }

        Map<String, Object> resolvedMap = new HashMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String resolvedKey = resolvePlaceholders(entry.getKey());
            Object resolvedValue = resolvePlaceholdersInObject(entry.getValue());
            resolvedMap.put(resolvedKey, resolvedValue);
        }
        return resolvedMap;
    }

    // 处理对象中的占位符
    public static Object resolvePlaceholdersInObject(Object obj) {
        if (obj == null) {
            return null;
        }

        if (obj instanceof String) {
            return resolvePlaceholders((String) obj);
        } else if (obj instanceof Map) {
            return resolvePlaceholdersInMap((Map<String, Object>) obj);
        } else if (obj instanceof List) {
            List<Object> list = (List<Object>) obj;
            return list.stream()
                    .map(PlaceholderResolver::resolvePlaceholdersInObject)
                    .collect(Collectors.toList());
        } else {
            // 其他类型直接返回
            return obj;
        }
    }

    // 将 Map 转换为 ApiConfig 对象
    public static ApiConfig mapToApiConfig(Map<String, Object> map) {
        ApiConfig config = new ApiConfig();
        config.setName(resolvePlaceholders((String) map.get("name")));
        config.setHost(resolvePlaceholders((String) map.get("host")));
        config.setPath(resolvePlaceholders((String) map.get("path")));
        config.setMethod(resolvePlaceholders((String) map.get("method")));

        // 处理 headers 中的占位符
        Map<String, String> rawHeaders = (Map<String, String>) map.get("headers");
        if (rawHeaders != null) {
            Map<String, String> resolvedHeaders = new HashMap<>();
            for (Map.Entry<String, String> entry : rawHeaders.entrySet()) {
                resolvedHeaders.put(resolvePlaceholders(entry.getKey()), resolvePlaceholders(entry.getValue()));
            }
            config.setHeaders(resolvedHeaders);
        }

        // 处理 body 中的占位符
        Map<String, Object> rawBody = (Map<String, Object>) map.get("body");
        if (rawBody != null) {
            Map<String, Object> resolvedBody = resolvePlaceholdersInMap(rawBody);
            config.setBody(resolvedBody);
        }

        // 处理 parameters 中的占位符
        Map<String, Object> rawParams = (Map<String, Object>) map.get("parameters");
        if (rawParams != null) {
            Map<String, String> resolvedParams = new HashMap<>();
            for (Map.Entry<String, Object> entry : rawParams.entrySet()) {
                resolvedParams.put(resolvePlaceholders(entry.getKey()), resolvePlaceholders(String.valueOf(entry.getValue())));
            }
            config.setParameters(resolvedParams);
        }

        // 处理 assertion 中的占位符
        Map<String, Object> rawAssertion = (Map<String, Object>) map.get("assertion");
        if (rawAssertion != null) {
            Map<String, Object> resolvedAssertion = resolvePlaceholdersInMap(rawAssertion);
            config.setAssertion(resolvedAssertion);
        }

        return config;
    }
}
